class OssWebPlugin {

    constructor(ossStsConfig) {
      this.key=ossStsConfig.key;
      this.ossStsConfig=  {
          "accessKeyId": ossStsConfig.accessKeyId,
          "securityToken": ossStsConfig.securityToken,
          "bucket": ossStsConfig.bucket,
          "baseOssUrl": ossStsConfig.baseOssUrl,
          "endpoint": ossStsConfig.endpoint,
          "accessKeySecret": ossStsConfig.accessKeySecret,
          "requestId": ossStsConfig.requestId,
          "expiration": ossStsConfig.expiration,
          "region": ossStsConfig.region,
      };
      this.ossClient=null;

    }

    //await M.getGloblePlugin('OssWebPlugin').list({prefix:'space1'})
    async list(query){
        return  this.ossClient.list(query);
    }

    /**
     * 文件上传
     * @param osskey  osskey
     * @param file    文件form
     * @param progress 进度
     * @returns {Promise<void>}
     */
    async multipartUpload(osskey, file,progress){
        let ossClient=this.ossClient;
        return new Promise((resolve,reject) => {
            ossClient.multipartUpload(osskey, file,{
                progress: function (p) { //上传进度设置
                    return function (done) {
                        progress(p)
                        done();
                    }
                }
            }).then(function (result) {
                resolve(result);
            }).catch(function (err) {
                reject(err);
            });
        })
    }

    async install(app,args){
        //引入oss库
        //https://github.com/ali-sdk/ali-oss?spm=a2c4g.11186623.0.0.10a424cbrpqCpg#summary
            M.cache.linkJs("https://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js");
            for (let i=0;i<5;i++){
                await M.delayMs(10);
                if(window.OSS){
                    break
                }
            }
            this.ossClient=new OSS.Wrapper({
                region: "oss-cn-beijing",
                accessKeyId: "AAA",
                accessKeySecret: "AAA",
                bucket: "ming-bucket-01"});
              MIO.ossMultipartUpload=this.multipartUpload.bind(this);
              MIO.ossClient=this.ossClient;
    }

}

export default  OssWebPlugin;

